package com.zhilei.controller;

import com.zhilei.base.BaseInfoProperties;
import com.zhilei.grace.result.GraceJSONResult;
import com.zhilei.pojo.Users;
import com.zhilei.pojo.bo.ModifyUserBO;
import com.zhilei.pojo.vo.UsersVO;
import com.zhilei.service.UsersService;
import com.zhilei.untils.GsonUtils;
import com.zhilei.untils.JWTUtils;
import com.zhilei.untils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 志磊
 * @since 2024-09-18
 */
@Slf4j
@RestController
@RequestMapping("userinfo")
public class UserInfoController extends BaseInfoProperties {

    @Autowired
    private UsersService usersService;

    @Autowired
    private JWTUtils jwtUtils;

    /**
     * 用户修改个人信息
     *
     * @param mod
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody ModifyUserBO mod) {

        // TODO 修改用户信息  --->  更新用户状态，重新生成token进行登录
        usersService.ModifyUserInfo(mod);

        // TODO 获取要修改的用户信息
        String userId = mod.getUserId();

        // TODO 查询并返回修改后的用户信息
        UsersVO userInfo = this.getUserInfo(userId, true);
        System.out.println(userInfo);

        // TODO 返回修改后的用户信息  --->  包含新生成的token
        return GraceJSONResult.ok(userInfo);
    }

    /**
     * 根据企业id查询当前公司下的HR数量
     *
     * @param companyId
     * @return
     */
    @PostMapping("getCountsByCompanyId")
    public GraceJSONResult getCountsByCompanyId(@RequestParam("companyId") String companyId) {

        Long hrCounts = usersService.getCountsByCompanyId(companyId);

        return GraceJSONResult.ok(hrCounts);

    }

    /**
     * 绑定hr跟企业之间的关系
     *
     * @param hrUserId
     * @param realname
     * @param companyId
     * @return
     */
    @PostMapping("bindingHRToCompany")
    public GraceJSONResult bindingHRToCompany(@RequestParam("hrUserId") String hrUserId,
                                              @RequestParam("realname") String realname,
                                              @RequestParam("companyId") String companyId) {

        // TODO 绑定hr跟企业之间的关系  --->  绑定状态，根据id修改真实姓名 电话号码
        usersService.bindingHRToCompany(hrUserId, realname, companyId);

        // TODO 返回给前端手机号
        Users userInfo = usersService.getUserInfo(hrUserId);
        String hrMobile = userInfo.getMobile();

        return GraceJSONResult.ok(hrMobile);

    }

    /**
     * 根据userId
     *
     * @param userId
     * @return
     */
    @PostMapping("freshUserInfo")
    public GraceJSONResult freshUserInfo(@RequestParam("userId") String userId) {
        UsersVO userVO = this.getUserInfo(userId, true);
        return GraceJSONResult.ok(userVO);

    }

    /**
     * 更新用户的状态为hr角色
     *
     * @param hrUserId
     * @return
     */
    @PostMapping("updateHRRole")
    public GraceJSONResult updateHRRole(@RequestParam("hrUserId") String hrUserId) {
        usersService.updateHRRole(hrUserId);
        return GraceJSONResult.ok();
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @PostMapping("get")
    public GraceJSONResult get(@RequestParam("userId") String userId) {
        UsersVO userVO = this.getUserInfo(userId, false);
        return GraceJSONResult.ok(userVO);

    }

    /**
     * hr身份转换
     *
     * @param hrUserId
     * @return
     */
    @PostMapping("changeUserToHR")
    public GraceJSONResult changeUserToHR(@RequestParam("hrUserId") String hrUserId) {
        usersService.uploadUserToHr(hrUserId);
        return GraceJSONResult.ok();

    }

    /**
     * 查询HR信息
     *
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("saas/hrList")
    public GraceJSONResult hrList(@RequestParam("page") Integer page,
                                  @RequestParam("limit") Integer limit) {
        PagedGridResult hrList = usersService.getHrList(page, limit);
        return GraceJSONResult.ok(hrList);

    }

    /**
     * 离职解绑员工信息
     *
     * @param hrUserId 用户id
     * @return
     */
    @PostMapping("changeUserToCand")
    public GraceJSONResult changeUserToCand(@RequestParam("hrUserId") String hrUserId) {
        usersService.changeUserToCand(hrUserId);
        return GraceJSONResult.ok();

    }

    /**
     * 登陆后查询用户信息
     *
     * @param userId
     * @return
     */
    private UsersVO getUserInfo(String userId, boolean jwtToken) {

        // TODO 查询最新的用户信息
        Users user = usersService.getUserInfo(userId);

        // TODO 将用户对象转换成json
        String newUserToJson = GsonUtils.object2String(user);

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user, usersVO);
        if (jwtToken) {
            // TODO 修改完个人信息重新登录系统，生成新的token
            String newUserToken = jwtUtils.createJWT(newUserToJson, BaseInfoProperties.TOKEN_USER_PREFIX);
            usersVO.setUserToken(newUserToken);
        }

        return usersVO;
    }
}
